package service

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	__ "server/basic/proto"
	"server/handler/models"
	"server/untils"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedHospitalServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user models.User
	user.Username = in.Username
	user.Password = in.Password
	if err := user.GetUserById(); err != nil {
		return nil, err
	}
	if user.Id == 0 {
		return nil, errors.New("该患者不存在，禁止登录")
	}
	if user.Password != in.Password {
		return nil, errors.New("密码错误，请重新输入")
	}
	return &__.LoginResp{
		Id: int64(user.Id),
	}, nil
}

// 问诊
func (s *Server) CreateConsultation(_ context.Context, in *__.CreateConsultationReq) (*__.CreateConsultationResp, error) {
	var doctor models.Doctor
	doctor.Id = uint(in.DoctorId)
	if err := doctor.GetUserById(); err != nil {
		return nil, err
	}
	if doctor.Id == 0 {
		return nil, errors.New("该用户无医生执业资格，无法接诊")
	}
	var user models.User
	user.Id = uint(in.UserId)
	if err := user.GetUserById(); err != nil {
		return nil, err
	}
	if user.Id == 0 {
		return nil, errors.New("患者信息与预约信息有误，禁止问诊")
	}
	consultation := models.Consultation{
		UserId:      uint(in.UserId),
		DoctorId:    uint(in.DoctorId),
		UserSymptom: in.UserSymptom,
	}
	if err := consultation.CreateConsultation(); err != nil {
		return nil, fmt.Errorf("接诊失败")
	}
	return &__.CreateConsultationResp{
		Id: int64(consultation.Id),
	}, nil
}

// 修改处方状态
func (s *Server) UpdateMedicine(_ context.Context, in *__.UpdateMedicineReq) (*__.UpdateMedicineResp, error) {
	var medicine models.Medicine
	medicine.Id = uint(in.Id)
	medicine.Status = uint(in.Status)
	if err := medicine.UpdateMedicine(); err != nil {
		return nil, fmt.Errorf("处方状态修改失败")
	}
	return &__.UpdateMedicineResp{
		Id: int64(medicine.Id),
	}, nil
}
func (s *Server) UserPay(_ context.Context, in *__.UserPayReq) (*__.UserPayResp, error) {
	var order models.Order
	order.Id = uint(in.OrderId)
	if err := order.GetOrderInfo(); err != nil {
		return nil, err
	}
	if order.Id == 0 {
		return nil, fmt.Errorf("该订单不存在")
	}
	aliPay := untils.Ali{}
	money := fmt.Sprintf("%.2d", order.Price)
	fmt.Println(order)
	return &__.UserPayResp{
		PayUrl: aliPay.Alipay(order.OrderSn, money),
	}, nil
}
func (s *Server) UpdateOrder(_ context.Context, in *__.UpdateOrderReq) (*__.UpdateOrderResp, error) {
	var order models.Order
	order.Id = uint(in.Id)
	order.OrderStatus = uint(in.Status)
	if err := order.GetOrderInfo(); err != nil {
		return nil, err
	}
	if order.Id == 0 {
		return nil, fmt.Errorf("该订单不存在")
	}
	if err := order.UpdateOrder(); err != nil {
		return nil, fmt.Errorf("订单状态修改失败")
	}
	return &__.UpdateOrderResp{
		Id: int64(order.Id),
	}, nil
}

// 创建订单
func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {
	order := models.Order{
		OrderSn:     uuid.NewString(),
		DocName:     in.DocName,
		DocRoom:     in.DocRoom,
		DocTitle:    in.DocTitle,
		UserName:    in.UserName,
		UserSymptom: in.UserSymptom,
		Price:       uint(in.Price),
	}
	if err := order.CreateOrder(); err != nil {
		return nil, fmt.Errorf("订单创建失败")
	}
	return &__.CreateOrderResp{
		Id: int64(order.Id),
	}, nil
}
